package com.wanho.java159.code.gen.util;

import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.Map;

/**
 * @author ChaoFei.Zhang
 * @date 2021-07-09
 */
public class FileUtil {

    /**
     * @param type 使用模板类型
     * @param data 填充数据
     * @throws IOException
     * @throws TemplateException
     */
    public static void generateToJava(GenCodeType type, Object data) throws IOException, TemplateException {
        Template tpl = getTemplate(type); // 获取模板文件
        // 填充数据
        StringWriter writer = new StringWriter();
        tpl.process(data, writer);
        writer.flush();
        String entityName = "" ;
        if (data instanceof Map){
            Map<String,String> map = (Map<String, String>) data;
            entityName = map.get("entity") ;
        }
        String fileName = "" ;
        switch (type) {
            case SERVICE:
                fileName = Common.SERVICE_DIR_PATH+entityName+"Service.java" ;
                break;
            case CONTROLLER:
                fileName =  Common.CONTROLLER_DIR_PATH+entityName+"Controller.java" ;
                break;
            case VUE_API:
                fileName =  Common.VUE_API_PATH+entityName+".js" ;
                break;
            case VUE_LIST:
                fileName =  Common.PAGE_PATH+entityName+"\\list.vue" ;
                break;
            case VUE_FORM:
                fileName =  Common.PAGE_PATH+entityName+"\\form.vue" ;
                break;
            case VUE_ROUTER:
                fileName =  Common.ROUTER_PATH+"index.js" ;
                break;
        }
        File file = new File(fileName);
        //判断文件夹是否存在
        if (!file.getParentFile().exists()){
            //动态创建文件夹
            file.getParentFile().mkdirs() ;
        }
        // 写入文件
        FileOutputStream fos = null ;
        //如果是路由类型文件尾部追加
        if (file.exists()){
            fos = new FileOutputStream(file,true) ;
        }else {
            fos = new FileOutputStream(file);
        }
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        BufferedWriter bw = new BufferedWriter(osw, 1024);
        tpl.process(data, bw);
        fos.close();
    }

    /**
     * 获取模板
     * @param type 模板类型
     * @return
     * @throws IOException
     */
    private static Template getTemplate(GenCodeType type) throws IOException {
        switch (type) {
            case ENTITY:
                return FreemarketConfigUtils.getInstance().getTemplate("Entity.ftl");
            case DAO:
                return FreemarketConfigUtils.getInstance().getTemplate("Dao.ftl");
            case SERVICE:
                return FreemarketConfigUtils.getInstance().getTemplate("Service.ftl");
            case CONTROLLER:
                return FreemarketConfigUtils.getInstance().getTemplate("Controller.ftl");
            case MAPPER:
                return FreemarketConfigUtils.getInstance().getTemplate("Mapper.ftl");
            case VUE_API:
                return FreemarketConfigUtils.getInstance().getTemplate("api.ftl");
            case VUE_LIST:
                return FreemarketConfigUtils.getInstance().getTemplate("list.ftl");
            case VUE_FORM:
                return FreemarketConfigUtils.getInstance().getTemplate("form.ftl");
            case VUE_ROUTER:
                return FreemarketConfigUtils.getInstance().getTemplate("router.ftl");
            default:
                return null;
        }
    }

}
